[#ftl]
[@define "agile._DataStore"]
	store:"",editable:false,fetched:false
	,onDownloadEnd:function(content){
		var time1 = new Date().getTime();
//		console.error("@@onDownloadEnd..1",this.title,0);		
		var containerNode = this.getContainerNode();
		query(">.agileComponent",containerNode).forEach(function(node){
			var widget = registry.byNode(node);
//			console.log("widget=",widget,node);
			try{
			if(!widget._destroyed){
				widget.destroyRecursive(false);
//				if(widget.destroyRecursive){
//					widget.destroyRecursive();
//				}else if(widget.destroy){
//					widget.destroy();
//				}
			}
			}catch(e){
				//TODO:fix error while destroying agile.layout.AccordionContainer 
				console.error("[_DataStore.onDownloadEnd]",widget.declaredClass,e);
				widget.domNode = null;
				widget = null;				
			}			
		},this);
	
//		query("[data-dojo-type]",containerNode).forEach(function(node){		
//			var widget = registry.byNode(node);
//			console.log("@@widget=",widget,widget._destroyed)			
//			if(!widget._destroyed){
//				//if(widget.destroyRecursive){
//					widget.destroyRecursive();
//				//}else if(widget.destroy){
//				//	widget.destroy();
//				//}
//				//construct.destroy(node);
//			}
//		},this);
//		var time2 = new Date().getTime();
//		console.log("@@onDownloadEnd..2",time2 - time1);
		this.beforeDataReady();
		
//		var time3 = new Date().getTime();
//		console.log("@@onDownloadEnd..3",time3 - time2);
		containerNode.innerHTML = content;		
		try{
//			console.log("@@onDownloadEnd..agile.form.DateTextBox",agile.form.DateTextBox);
			agile.requireClazz(containerNode);			
//			var time4 = new Date().getTime();			
//			console.log("@@onDownloadEnd..5",time4 - time3);
			parser.parse(containerNode);
//			var time5 = new Date().getTime();
//			console.log("@@onDownloadEnd..6",time5-time4);
			this.onDataReady();
//			var time6 = new Date().getTime();
//			console.log("@@onDownloadEnd..7",time6 - time5);
			this.fetched = true;
						
			[#if localStorage]
				if(window["ls"]){
					 window["ls"][this.store] = content;
				}
			[/#if]
		}catch(e){
			//topic.publish("agile.message",["error",null,"Error occurred while parsing source",e]);
			console.error("Error occurred while parsing source",e);
		}
	}
	[@method "getContainerNode"/]
	,getContainerNode:function(){
		if(this.containerNode == null){
			console.error(this.declaredClass,"containerNode undefined");
		}
		return this.containerNode;
	}
	,beforeDataReady:function(){		
	}
	,onDataReady:function(){		
	}
	,save:function(target,content,draft){	
		if(!this.editable){
//			alert("ReadOnly");
//			topic.publish("agile.message","info",null,agile.i18n("msg_readOnly"),null);
			return;
		}
		
		if(window["ls"]){
			window["ls"][target] = content;	
			[#if !hasServerResource]
			topic.publish("agile.message","info",null,agile.i18n("msg_local_save_ok"),null);
			[/#if]
		}
		//save to server for final version
	[#if hasServerResource]
		if(!draft){
			var uri = "${webAppHost}${context}/${project}/save.do?target="+encodeURIComponent(target);
			request.post(uri,{content:{content:encodeURIComponent(content)},handleAs:"text"})
	//			headers: {"Content-Type":"text/plain","Content-Encoding":"utf-8"},
		    	.then(
		    			function(resp,ioArgs){
		//				//console.log("data has been saved to ",resp);											
						topic.publish("agile.message","info",null,agile.i18n("msg_server_save_ok"),null);
					}
			        ,function(data){
		//				//console.log("error occurred:",data);				
						topic.publish("agile.message","error",null,agile.i18n("msg_server_save_error"),data);
					}
		        );		   
		}
	[/#if]
	}
	
[/@]